package com.o2opuzi.pub.controller

import com.alibaba.fastjson.JSON;
import com.o2opuzi.pub.component.AppComponent;
import com.o2opuzi.pub.dao.ConfigDao

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin/system")
class SystemInfoConfigController {
	
	@Autowired
	private ConfigDao configDao; 
	@Autowired
	private AppComponent appComponent;
	
	/**
	 * 获取系统信息
	 * @auther Y.hj
	 * @return
	 */
	@GetMapping("/info")
	def info(){
		def config = configDao.getConfig("system_info");
		def system_info = [:];
		if(config != null){
			system_info = JSON.parseObject(config.config_value);
		}
		return [
			code:1,
			data:[
				system_info:system_info
			]
		];	
	}
	
	
	/**
	 * 保存信息
	 * @auther Y.hj
	 * @param config
	 * @return
	 */
	@PostMapping("/info")
	def info(@RequestBody Map config){
		if(StringUtils.isEmpty(config["name"])){
			return [code:0,messages:[text:"系统名称不能为空"]];
		}		
		def conf =  configDao.getConfig("system_info");
		String value = JSON.toJSONString(config);
		if(conf == null){
			configDao.save([
				config_key:"system_info",
				config_value:value,
				tip:"系统配置"
			]);
		}else{
			configDao.update([ 
				"config_value":value
			], conf.id);
		}
		//重新加载配置文件
		appComponent.loadConfig();
		return [
			code:1,
			messages:[text:"保存成功"]
		];
	}
}
